package com.skcc.cop.mrtaxi.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

public class CalendarUtil {

	public static GregorianCalendar getTodayCalendar() {
		Calendar cal = Calendar.getInstance();
		GregorianCalendar reCal = new GregorianCalendar();
		reCal.set(Calendar.YEAR, cal.get(Calendar.YEAR));
		reCal.set(Calendar.MONTH, cal.get(Calendar.MONTH));
		reCal.set(Calendar.DAY_OF_MONTH, cal.get(Calendar.DAY_OF_MONTH));
		return reCal;
	}

	/**
	 * @return 2013-01-17 09:00:00
	 */
	public static String getCurrentTime() {
		return intoString(getTodayCalendar());
	}

	public static int[] getTodayArray() {
		int[] reArray = new int[3];
		Calendar cal = Calendar.getInstance();
		reArray[0] = cal.get(Calendar.YEAR);
		reArray[1] = cal.get(Calendar.MONTH) + 1;
		reArray[2] = cal.get(Calendar.DAY_OF_MONTH);
		return reArray;
	}
	
	public static int[] getYesterdayArray() {
		int[] reArray = new int[3];
		Calendar cal = Calendar.getInstance();
		cal.add(Calendar.DAY_OF_MONTH, -1);
		reArray[0] = cal.get(Calendar.YEAR);
		reArray[1] = cal.get(Calendar.MONTH) + 1;
		reArray[2] = cal.get(Calendar.DAY_OF_MONTH);
		return reArray;
	}

	/**
	 * @return 2013-01-17
	 */
	public static String getTodayString() {
		return getTodayTimeString().substring(0, 10);
	}
	
	public static String getTodayTimeString() {
		Calendar cal = Calendar.getInstance();
		GregorianCalendar reCal = new GregorianCalendar();
		reCal.set(Calendar.YEAR, cal.get(Calendar.YEAR));
		reCal.set(Calendar.MONTH, cal.get(Calendar.MONTH));
		reCal.set(Calendar.DAY_OF_MONTH, cal.get(Calendar.DAY_OF_MONTH));
		return intoString(reCal);
	}

	public static boolean isToday(GregorianCalendar calInput) {
		GregorianCalendar reCal = new GregorianCalendar();
		if (calInput.get(Calendar.YEAR) != reCal.get(Calendar.YEAR)) {
			return false;
		} else if (calInput.get(Calendar.MONTH) != reCal.get(Calendar.MONTH)) {
			return false;
		} else if (calInput.get(Calendar.DAY_OF_MONTH) != reCal.get(Calendar.DAY_OF_MONTH)) {
			return false;
		}
		return true;
	}

	public static boolean isToday(String toData) {
		if (toData.equals("0")) {
			return false;
		}
		Calendar thisCal = Calendar.getInstance();
		int year = Integer.parseInt(toData.subSequence(0, 4).toString());
		int month = Integer.parseInt(toData.subSequence(5, 7).toString());
		int day = Integer.parseInt(toData.subSequence(8, 10).toString());

		if (year == thisCal.get(Calendar.YEAR) && month == (thisCal.get(Calendar.MONTH) + 1)
				&& day == thisCal.get(Calendar.DAY_OF_MONTH)) {
			return true;
		} else {
			return false;
		}
	}

	public static String intoString(GregorianCalendar inCal) {
		return intoString(inCal.get(Calendar.YEAR), inCal.get(Calendar.MONTH) + 1, inCal.get(Calendar.DAY_OF_MONTH),
				inCal.get(Calendar.HOUR_OF_DAY), inCal.get(Calendar.MINUTE), inCal.get(Calendar.SECOND));
	}

	public static String intoString(long time) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		return sdf.format(new Date(time));
	}
	
	public static long intoLong(String time) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date date;
		try {
			date = sdf.parse(time);
		} catch (ParseException e) {
			return 0;
		} 
		Calendar gc = new GregorianCalendar();
		gc.setTime(date);
		return gc.getTimeInMillis();
	}


	public static String intoString() {
		long nowmills = System.currentTimeMillis();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		return sdf.format(new Date(nowmills));
	}

	public static String intoString(int year, int month, int day, int hour, int min, int sec) {
		// 2012-09-01 14:00:00
		String reStr = String.valueOf(year) + "-" + addFirstZero(month) + "-" + addFirstZero(day) + " "
				+ addFirstZero(hour) + ":" + addFirstZero(min) + ":" + addFirstZero(sec);
		return reStr;
	}

	// public static String getDataTime() {
	// Calendar cal = Calendar.getInstance();
	// String dateToString, timeToString;
	// dateToString = String.format("%04d-%02d-%02d", cal.get(Calendar.YEAR),
	// cal.get(Calendar.MONTH) + 1,
	// cal.get(Calendar.DAY_OF_MONTH));
	// timeToString = String.format("%02d:%02d:%02d",
	// cal.get(Calendar.HOUR_OF_DAY), cal.get(Calendar.MINUTE),
	// cal.get(Calendar.SECOND));
	// return dateToString + " " + timeToString;
	// }

	public static String intoString(int hour, int minite) {
		return addFirstZero(hour) + ":" + addFirstZero(minite);
	}

	public static String addFirstZero(int value) {
		if (value < 10) {
			return "0" + String.valueOf(value);
		} else {
			return String.valueOf(value);
		}
	}

	public static int getTimeGapSecond(String inTime, String outTime) {
		GregorianCalendar inCal = intoCalendar(inTime);
		GregorianCalendar outCal = intoCalendar(outTime);
		long millis = inCal.getTimeInMillis() - outCal.getTimeInMillis();
		return Math.abs((int) (millis / 1000.0));
	}
	
	public static boolean isAfterHourMinute(String inTime, String outTime) {
		GregorianCalendar inCal = intoCalendar(inTime);
		inCal.set(Calendar.MILLISECOND, 0);
		GregorianCalendar outCal = intoCalendar(outTime);
		outCal.set(Calendar.MILLISECOND, 0);
		long millis = outCal.getTimeInMillis() - inCal.getTimeInMillis();
		if(millis > 0){
			return true;
		}
		return false;
	}
	
	public static boolean isAfterTime(String inTime, String outTime) {
		GregorianCalendar inCal = intoCalendar(inTime);
		GregorianCalendar outCal = intoCalendar(outTime);
		long millis = outCal.getTimeInMillis() - inCal.getTimeInMillis();
		if(millis > 0){
			return true;
		}
		return false;
	}

	public static GregorianCalendar intoCalendar(String toDate) {
		GregorianCalendar inCal = new GregorianCalendar();

		inCal.set(Integer.parseInt(toDate.subSequence(0, 4).toString()),
				Integer.parseInt(toDate.subSequence(5, 7).toString()) - 1,
				Integer.parseInt(toDate.subSequence(8, 10).toString()),
				Integer.parseInt(toDate.subSequence(11, 13).toString()),
				Integer.parseInt(toDate.subSequence(14, 16).toString()),
				Integer.parseInt(toDate.subSequence(17, 19).toString()));

		return inCal;
	}
}
